package loadbanlance

import (
	"go-gw-scaffold/api/loadbanlance/inst"
	"log"
)

type Banlancer interface {
	Add(remote string) error
	Next() string
	Get() (string, error)
}

type LoadbanlanceManager struct {
	banlanceMap map[string]Banlancer
}

func NewLoadbanlanceManager() *LoadbanlanceManager {
	return &LoadbanlanceManager{
		banlanceMap: map[string]Banlancer{},
	}
}

func (_this *LoadbanlanceManager) Add(name string, instance Banlancer) {
	if name == "" {
		log.Fatal("name is empty")
		return
	}
	if instance == nil {
		log.Fatal("instance is nil")
		return
	}
	_, ok := _this.banlanceMap[name]
	if ok {
		log.Fatal("name is invalid")
		return
	}
	_this.banlanceMap[name] = instance
}

func (_this *LoadbanlanceManager) Get(name string) Banlancer {
	if name == "" {
		log.Fatal("name is empty")
		return nil
	}
	_, ok := _this.banlanceMap[name]
	if ok {
		return _this.banlanceMap[name]
	}
	return inst.NewRandomBanlance()
}
